//
//  WYCanLeadView.m
//  Boss
//
//  Created by admin on 2018/8/14.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYCanLeadView.h"
#import "AddressPickerHeader.h"
#import "UIButton+CZHExtension.h"
#define TOOLBAR_BUTTON_WIDTH CZH_ScaleWidth(65)

typedef NS_ENUM(NSInteger, CZHCanPickerViewButtonType) {
    CZHCanPickerViewButtonTypeCancle,
    CZHCanPickerViewButtonTypeSure
};
@interface WYCanLeadView()<UIPickerViewDelegate, UIPickerViewDataSource>
@property (nonatomic,strong)UIPickerView *pickerView;
@property (nonatomic,strong)NSArray *dataArray;
@property (nonatomic,strong)UIView *containView;
@property (nonatomic, copy) void (^canBlock)(NSString *name, NSString *accurateType,NSString *vipLevel);

@property (nonatomic,strong) NSString *accurateType;
@property (nonatomic,strong) NSString *vipLevel;
@property (nonatomic,strong) NSString *name;

@end
@implementation WYCanLeadView
-(NSArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = @[@"不限",@"帅哥专享",@"美女专享",@"BOSS团",@"白银专享",@"黄金专享",@"钻石专享"];
    }
    return _dataArray;
}
+ (instancetype)showWhoCanView:(NSString *)normal CanBlock:(void(^)(NSString *name, NSString *accurateType,NSString *vipLevel))canBlock
{
    WYCanLeadView *_view = [[WYCanLeadView alloc]init];
    _view.canBlock = canBlock;
    _view.accurateType = @"0";
    _view.vipLevel = @"0";
    _view.name = @"不限";
    [_view showView];
    return _view;
}


-(instancetype)init
{
    if (self = [super init]) {
        [self createSubViews];
    }
    return self;
}
-(void)createSubViews
{
    self.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    
    UIView *containView = [[UIView alloc] init];
    containView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, CZH_ScaleHeight(270));
    [self addSubview:containView];
    self.containView = containView;
    
    
    UIView *toolBar = [[UIView alloc] init];
    toolBar.frame = CGRectMake(0, 0, ScreenWidth, CZH_ScaleHeight(55));
    toolBar.backgroundColor = CZHColor(0xf6f6f6);
    [containView addSubview:toolBar];
    
    UIButton *cancleButton = [UIButton czh_buttonWithTarget:self action:@selector(buttonClick:) frame:CGRectMake(0, 0, TOOLBAR_BUTTON_WIDTH, toolBar.czh_height) titleColor:CZHColor(0x666666) titleFont:CZHGlobelNormalFont(18) title:@"取消"];
    cancleButton.tag = CZHCanPickerViewButtonTypeCancle;
    [toolBar addSubview:cancleButton];
    
    UIButton *sureButton = [UIButton czh_buttonWithTarget:self action:@selector(buttonClick:) frame:CGRectMake(toolBar.czh_width - TOOLBAR_BUTTON_WIDTH, 0, TOOLBAR_BUTTON_WIDTH, toolBar.czh_height) titleColor:CZHThemeColor titleFont:CZHGlobelNormalFont(18) title:@"确定"];
    sureButton.tag = CZHCanPickerViewButtonTypeSure;
    [toolBar addSubview:sureButton];
    
    
    UIPickerView *pickerView = [[UIPickerView alloc] init];
    pickerView.backgroundColor = CZHColor(0xffffff);
    pickerView.frame = CGRectMake(0, toolBar.czh_bottom, ScreenWidth, containView.czh_height - toolBar.czh_height);
    pickerView.delegate = self;
    pickerView.dataSource = self;
    [containView addSubview:pickerView];
    self.pickerView = pickerView;
}

- (void)buttonClick:(UIButton *)sender {
    [self hideView];
//
    if (sender.tag == CZHCanPickerViewButtonTypeSure) {
        if (self.canBlock) {
            self.canBlock(self.name, self.accurateType, self.vipLevel);
        }
    }
}
#pragma mark -- UIPickerView

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.dataArray.count;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width / 3, 30)];
    label.adjustsFontSizeToFitWidth = YES;
    label.textAlignment = NSTextAlignmentCenter;
    label.text = self.dataArray[row];
    return label;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    self.name = self.dataArray[row];
    switch (row) {
        case 0:
            self.vipLevel = @"0";
            self.accurateType = @"0";
            break;
        case 1:
            self.vipLevel = @"0";
            self.accurateType = @"1";
            break;
        case 2:
            self.vipLevel = @"0";
            self.accurateType = @"2";
            break;
        case 3:
            self.vipLevel = @"0";
            self.accurateType = @"3";
            break;
        case 4:
            self.vipLevel = @"10";
            self.accurateType = @"0";
            break;
        case 5:
            self.vipLevel = @"20";
            self.accurateType = @"0";
            break;
        case 6:
            self.vipLevel = @"30";
            self.accurateType = @"0";
            break;
        default:
            break;
    }
}
- (void)showView {
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    self.backgroundColor = [UIColor clearColor];
    
    [UIView animateWithDuration:0.3 animations:^{
        self.backgroundColor = CZHRGBColor(0x000000, 0.3);
        self.containView.czh_bottom = ScreenHeight;
    }];
}

- (void)hideView {
    
    [UIView animateWithDuration:0.3 animations:^{
        self.backgroundColor = [UIColor clearColor];
        self.containView.czh_y = ScreenHeight;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 40.0;
}

@end
